package jp.sourceforge.akjrcp.text_douga.video;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import jp.sourceforge.akjrcp.text_douga.AudioData;
import jp.sourceforge.akjrcp.text_douga.DougaSystem;
import jp.sourceforge.akjrcp.text_douga.SystemEnvironment;
import jp.sourceforge.akjrcp.text_douga.VideoData;
import jp.sourceforge.akjrcp.text_douga.DougaPreference;
import jp.sourceforge.akjrcp.text_douga.VideoWriter;
import jp.sourceforge.akjrcp.text_douga.effect.VideoEffect;

public class ImageVideoData extends EffectableVideoData{
	private String inputPath;

	private double duration=3.0;
	//private double loopPadTime;//loop only
	//private int loop;
	//private boolean canStopInLoop=true;//don't use
	private AudioData audioData;
	//private int padColor;
	
	
	private boolean fitSize=true;
	private int bgColor=0;

	public String toString(){
	return "ImageVideoData:duration="+duration+" inputPath="+inputPath;	
	}
	public ImageVideoData(String inputPath){
		this.inputPath=inputPath;
	}

	public double getDuration() {
		return duration;
	}
	public void setDuration(double duration) {
		this.duration = duration;
	}
	public String getInputPath() {
		return inputPath;
	}
	public void setInputPath(String inputPath) {
		this.inputPath = inputPath;
	}


	public int writeData(File dir,DougaPreference preference,int startIndex,int maxframe) throws IOException {
		int wf=0;
		
		if(startEffect!=null){
			startEffect.setKeyFrame(startIndex);
		}
		
		BufferedImage bgImage=SystemEnvironment.getSystemEnvironment().getImageCasher().loadImage(inputPath);
	
		if(bgImage!=null){
			double ratioW=preference.getWidth()/bgImage.getWidth();
			double ratioH=preference.getHeight()/bgImage.getHeight();
			boolean useW=true;
			if(ratioW>ratioH){
				useW=false;
			}
			int dw;
			int dh;
			if(useW){
				dw=preference.getWidth();
				dh=(int) (preference.getHeight()*ratioW);
			}else{
				dw=(int) (preference.getWidth()*ratioH);
				dh=preference.getHeight();
			}
			int dx=(preference.getWidth()-dw)/2;
			int dy=(preference.getHeight()-dh)/2;
			
			BufferedImage frameImage=new BufferedImage(preference.getWidth(),preference.getHeight(),BufferedImage.TYPE_INT_RGB);//JPEG must RGB?
			Graphics2D g2=(Graphics2D) frameImage.getGraphics();
			g2.setColor(new Color(bgColor));
			g2.fillRect(0, 0, preference.getWidth(), preference.getHeight());
			g2.drawImage(bgImage, dx, dy, dw, dh, 0, 0, bgImage.getWidth(), bgImage.getHeight(), null);
			
			int frames=0;
			if(duration==DougaSystem.FOREEVER){
				frames=maxframe;//draw all
				}else{
					frames=(int) (preference.getFps()*duration);
					if(maxframe!=0 && maxframe<frames){
						frames=maxframe;
					}	
				}
			
			wf=VideoUtils.copyImage(frameImage, dir, startIndex, frames);
			
		}else{
			throw new IOException("bgimage can't load:"+inputPath);
			
		}
		//sync audio
		if(audioData!=null){
			audioData.setSynchLength(wf/preference.getFps());
		}
		
		
		//TODO if video is too short ,bad things will happen.
		if(wf>0){
			
			if(endEffect!=null){
				endEffect.setKeyFrame(startIndex+wf-1);
			}
		}else{//0 writed
			if(startEffect!=null){
				startEffect.setKeyFrame(VideoEffect.NULL_TIME);//no writed.
			}
		}
		
		return wf;
	}
	public AudioData getAudioData() {
		return audioData;
	}
	public void setAudioData(AudioData audioData) {
		this.audioData = audioData;
	}
	public int getBgColor() {
		return bgColor;
	}
	public void setBgColor(int bgColor) {
		this.bgColor = bgColor;
	}
	public boolean isFitSize() {
		return fitSize;
	}
	public void setFitSize(boolean fitSize) {
		this.fitSize = fitSize;
	}
	
	public double getTotalTime() {
		
		return duration;
	}
	public int writeData(VideoWriter writer, DougaPreference preference) throws IOException {
		int maxframe=Math.max(0, writer.getMaxFrame()-writer.getWrited());
		int startIndex=writer.getWrited()+1;
		
		if(startEffect!=null){
			startEffect.setKeyFrame(startIndex);
		}
		
		int wf=0;
		BufferedImage bgImage=SystemEnvironment.getSystemEnvironment().getImageCasher().loadImage(inputPath);
	
		if(bgImage!=null){
			double ratioW=preference.getWidth()/bgImage.getWidth();
			double ratioH=preference.getHeight()/bgImage.getHeight();
			boolean useW=true;
			if(ratioW>ratioH){
				useW=false;
			}
			int dw;
			int dh;
			if(useW){
				dw=preference.getWidth();
				dh=(int) (preference.getHeight()*ratioW);
			}else{
				dw=(int) (preference.getWidth()*ratioH);
				dh=preference.getHeight();
			}
			int dx=(preference.getWidth()-dw)/2;
			int dy=(preference.getHeight()-dh)/2;
			
			BufferedImage frameImage=new BufferedImage(preference.getWidth(),preference.getHeight(),BufferedImage.TYPE_INT_RGB);//JPEG must RGB?
			Graphics2D g2=(Graphics2D) frameImage.getGraphics();
			g2.setColor(new Color(bgColor));
			g2.fillRect(0, 0, preference.getWidth(), preference.getHeight());
			g2.drawImage(bgImage, dx, dy, dw, dh, 0, 0, bgImage.getWidth(), bgImage.getHeight(), null);
			
			int frames=0;
			if(duration==DougaSystem.FOREEVER){
				frames=maxframe;//draw all
				}else{
					frames=(int) (preference.getFps()*duration);
					if(maxframe!=0 && maxframe<frames){
						frames=maxframe;
					}	
				}
			
			wf=VideoUtils.copyImage(frameImage, this,writer,  frames);
			
		}else{
			throw new IOException("bgimage can't load:"+inputPath);
			
		}
		//sync audio
		if(audioData!=null){
			audioData.setSynchLength(wf/preference.getFps());
		}
		
		
//		TODO if video is too short ,bad things will happen.
		if(wf>0){
			
			if(endEffect!=null){
				endEffect.setKeyFrame(startIndex+wf-1);
			}
		}else{//0 writed
			if(startEffect!=null){
				startEffect.setKeyFrame(VideoEffect.NULL_TIME);//no writed.
			}
		}
		
		return wf;
	}

}
